<template>
  <c-dialog ref="dialog" :open-fun="openFun" title="任务作废" @close="closeHandle">
    <c-list ref="form" :model="formData" :rules="formRules" :column="2" label-width="120px">
      <c-list-item label="门店建设项目号" :value="detail.constCode" />
      <c-list-item label="装修任务号" :value="detail.decorationTaskCode" />
      <c-list-item label="当前装修进度" :value="detail.schedule" dict-type-code="DECORATION_SCHEDULE" />
      <c-list-item label="门店编码" :value="detail.shopCode" />
      <c-list-item label="门店名称" :value="detail.shopName" />
      <c-list-item label="所属客户编码" :value="detail.clientCode" />
      <c-list-item label="所属客户" :span="2" :value="detail.clientName" />
      <c-list-item label="所属办事处" :span="2" :value="detail.officeName" />
      <c-list-item label="终止原因" :span="2" prop="endReason">
        <el-input v-model="formData.endReason" type="textarea" style="width:100%" :autosize="{minRows:3}" placeholder="终止原因" clearable />
      </c-list-item>
      <c-list-item label="申请人" :value="$store.getters.currUser.userName" />
      <c-list-item label="申请时间" :value="getCurrentDate" />
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postInvalidPro } from '@/api/decorate'
import { parseTime } from '@/utils'
export default {
  name: 'Cancellation',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      formData: {
        endReason: ''
      },
      formRules: {
        endReason: [{ required: true, message: '请输入终止原因', trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length === 1 ? this.selData[0] : {}
    },
    getCurrentDate() {
      return parseTime(new Date(), '{y}-{m}-{d}')
    }
  },
  methods: {
    openFun() {
      if (!this.singleJudge(this.selData, '任务作废', true)) {
        return false
      }
      const s = this.selData[0]
      if (['H', 'I', 'J', 'K', 'M', 'L'].includes(s.schedule)) {
        this.$message.warning('装修完工，验收申请、验收执行、整改、已结案、已终止的任务不能操作任务作废按钮！')
        return false
      }
      return true
    },
    confirmHandle() {
      this.$refs.form.validate().then(() => {
        const data = this.selData[0]
        data.endReason = this.formData.endReason
        data.applicantCode = this.$store.getters.currUser.account
        data.applicantName = this.$store.getters.currUser.userName
        data.applicantTime = this.getCurrentDate + ' 00:00:00'
        return postInvalidPro(data).then(res => {
          this.$message.success('任务作废成功！')
          this.$refs.dialog.visible = false
          this.$emit('on-success')
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    },
    closeHandle() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
